<?php

namespace App\Controllers\v1;

use App\Models\UserLog;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use common\utils\SysCode;
use App\Controllers\BaseController;
use App\Services\DemoService;

class DemoController extends BaseController
{
    public $allows = ["getDemoUser","register"];

    /**
     * 试用会员注册
     */
    public function register(Request $request)
    {
        $params = $this->validate($request, [
            'username' => 'required',
            'password' => 'required'
        ]);
        if(!preg_match("/^([0-9a-zA-Z]{6,16})$/i", $params['password'])){
            error_exit(SysCode::DEMO_USERNAME_PASSWORD_ERROR);
        }
        return DemoService::register($params);
    }

    /**
     * 获取试玩账户
     */
    public function getDemoUser(Request $request)
    {
        $demoName = 'test_'.rand(1,999999);
        if(DemoService::isExistName($demoName)){
            error_exit(SysCode::DEMO_USERNAME_EXIST);
        }else{
            return $demoName;
        }
    }

}
